package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.JhdwDao;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-5
 * Time: 上午10:55
 * To change this template use File | Settings | File Templates.
 */
@Service
public class JhdwService {
    private JhdwDao jhdwDao;
    private BmService bmService;

    @Transactional
    public String findAllJhdw(String qc,String jc,String lxr,int pageNo, int pageSize) {
        Page<Qyjg> jhdwPage = this.jhdwDao.findJhdwList(qc,jc,lxr,pageNo, pageSize);
        List<Qyjg> jhdwList = jhdwPage.getResult() == null ? new ArrayList<Qyjg>() : jhdwPage.getResult();
        String jhdwJson = BaseUtils.fetchCollectionToJson(jhdwList, "id", "qc", "jc", "bm", "zjm", "bz", "sfzx", "sfls", "qyfr.qyqc", "qyfr.id", "lxdh", "lxr", "jyms.id", "jyms.bz");
        return "{\"total\":" + jhdwPage.getTotalCount() + ",\"rows\":[" + jhdwJson + "]}";
    }

    //查询计划单位详细信息
    @Transactional
    public Qyjg findJhdwDetail(Long id) {
        return this.jhdwDao.get(id);
    }

    // 保存计划单位
    @Transactional
    public void saveJhdw(Qyjg jhdw) throws RuntimeException {
        String  bm = this.getJhdwbm();
        jhdw.setBm(bm);
        jhdw.setDateCreated(new Date());
        jhdw.setLastUpdated(jhdw.getDateCreated());
        this.jhdwDao.save(jhdw);
    }
    public String getJhdwbm() {
        return bmService.getJgbm();
    }
    //删除计划单位
    @Transactional
    public void deleteJhdw(Long id) {
        Qyjg jhdw = jhdwDao.get(id);
        if (!jhdw.getSyljList().isEmpty() || !jhdw.getXyljList().isEmpty()) {
            throw new RuntimeException("禁止删除！该计划单位已在路径维护中使用");
        }
        if (!jhdw.getKhhList().isEmpty()) {
            throw new RuntimeException("禁止删除！该计划单位已在企业开户行中使用");
        }
        if (!jhdw.getSfzx()) {
            throw new RuntimeException("禁止删除！计划单位已启用");
        }
        jhdw.setLastUpdated(new Date());
        jhdw.setDeleted(1);
        this.jhdwDao.save(jhdw);
    }

    //修计划单位
    @Transactional
    public void updateJhdw(Qyjg jhdw) {
        Qyjg clone = this.findJhdwDetail(jhdw.getId());
        clone = (Qyjg) ReflectionUtils.getInstance(clone, jhdw);
        clone.setLastUpdated(new Date());
        this.jhdwDao.save(clone);
    }

    public boolean ajaxValidate(Long id, String value, String field) {
        return jhdwDao.checkUnique(id, value, field);
    }

    /**
     * 启动计划单位
     *
     * @param id
     */
    @Transactional
    public void startJhdw(Long id) {
        Qyjg jhdw = jhdwDao.get(id);
        jhdw.setLastUpdated(new Date());
        jhdw.setSfzx(false);
        this.jhdwDao.save(jhdw);
    }

    /**
     * 停用计划单位
     *
     * @param id
     */
    @Transactional
    public void stopJhdw(Long id) {
        Qyjg jhdw = jhdwDao.get(id);
        if (!jhdw.getSyljList().isEmpty() || !jhdw.getXyljList().isEmpty()) {
            throw new RuntimeException("禁止停用！该计划单位已在路径维护中使用");
        }
        jhdw.setLastUpdated(new Date());
        jhdw.setSfzx(true);
        this.jhdwDao.save(jhdw);
    }

    @Autowired
    public void setJhdwDao(JhdwDao jhdwDao) {
        this.jhdwDao = jhdwDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
